package com.yugong.service.impl;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yugong.common.Const;
import com.yugong.entity.dto.*;
import com.yugong.entity.vo.request.AccountRequestVO;
import com.yugong.entity.vo.response.AccountListResponseVO;
import com.yugong.exception.CustomException;
import com.yugong.mapper.*;
import com.yugong.service.AccountService;
import com.yugong.util.BrowserUtils;
import com.yugong.util.DigestUtils;
import com.yugong.util.SystemUtils;
import com.yugong.util.TimeUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 账户业务逻辑实现类
 *
 * @author yugong
 * @date 2024/2/9
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {
    @Resource
    private AccountMapper accountMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private BusinessMapper businessMapper;
    @Resource
    private OrderClassAdminMapper orderClassAdminMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private DigestUtils digestUtils;
    @Resource
    private TimeUtils timeUtils;
    @Resource
    private BrowserUtils browserUtils;
    @Resource
    private SystemUtils systemUtils;

    @Value("${ip}")
    private String ip;
    @Value("${server.port}")
    private String port;

    /**
     * 用户登录
     *
     * @param accountRequestVO 登录信息
     * @return 用户信息
     */
    @Override
    public AccountListResponseVO accountLogin(AccountRequestVO accountRequestVO) {
        if (accountRequestVO != null) {
            LambdaQueryWrapper<Account> accountLambdaQueryWrapper = new LambdaQueryWrapper<>();
            accountLambdaQueryWrapper
                    .eq(Account::getAccountNo, accountRequestVO.getAccountNo());
            Account accountLogin = accountMapper.selectOne(accountLambdaQueryWrapper);
            if (accountLogin == null) {
                // 没有用户
                throw new CustomException("账号或密码错误");
            }
            // 比较密码
            if (!digestUtils.md5Digest(accountRequestVO.getAccountPassword()).equals(accountLogin.getAccountPassword())) {
                // 密码错误
                throw new CustomException("账号或密码错误");
            }
            Role role = roleMapper.selectById(accountLogin.getAccountRoleNo());
            String roleName = role.getRoleName();
            AccountListResponseVO accountListResponseVO = new AccountListResponseVO();
            accountListResponseVO.setAccountNo(accountLogin.getAccountNo());
            String accountPhoto = accountLogin.getAccountPhoto();
            accountListResponseVO.setAccountPhoto(accountPhoto);
            accountListResponseVO.setAccountEmail(digestUtils.base64Decode(accountLogin.getAccountEmail()));
            if (Const.ROLE_SYSTEM_ADMIN.equals(roleName)) {
                accountListResponseVO.setAccountName(accountLogin.getAccountNo());
            } else if (Const.ROLE_COMMON_STUDENT.equals(roleName)) {
                Student student = studentMapper.selectById(accountLogin.getAccountNo());
                accountListResponseVO.setAccountName(student.getStudentName());
            } else if (Const.ROLE_COMMON_TEACHER.equals(roleName)) {
                Teacher teacher = teacherMapper.selectById(accountLogin.getAccountNo());
                accountListResponseVO.setAccountName(teacher.getTeacherName());
            } else if (Const.ROLE_COMMON_BUSINESS.equals(roleName)) {
                Business business = businessMapper.selectById(accountLogin.getAccountNo());
                accountListResponseVO.setAccountName(business.getBusinessOrderClassHeadName());
            } else if (Const.ROLE_COMMON_ORDER_CLASS_ADMIN.equals(roleName)) {
                OrderClassAdmin orderClassAdmin = orderClassAdminMapper.selectById(accountLogin.getAccountNo());
                accountListResponseVO.setAccountName(orderClassAdmin.getOrderClassAdminName());
            } else if (Const.ROLE_VISITOR_ADMIN.equals(roleName)) {
                accountListResponseVO.setAccountName("游客-管理员");
            } else if (Const.ROLE_VISITOR_STUDENT.equals(roleName)) {
                accountListResponseVO.setAccountName("游客-学生");
            } else if (Const.ROLE_VISITOR_TEACHER.equals(roleName)) {
                accountListResponseVO.setAccountName("游客-教师");
            } else if (Const.ROLE_VISITOR_BUSINESS.equals(roleName)) {
                accountListResponseVO.setAccountName("游客-企业订单班负责人");
            } else if (Const.ROLE_VISITOR_ORDER_CLASS_ADMIN.equals(roleName)) {
                accountListResponseVO.setAccountName("游客-订单班管理员");
            }
            accountListResponseVO.setAccountRoleNo(accountLogin.getAccountRoleNo());
            accountListResponseVO.setAccountRoleName(roleName);
            accountListResponseVO.setAccountPhone(digestUtils.base64Decode(accountLogin.getAccountPhone()));
            return accountListResponseVO;
        } else {
            return null;
        }
    }

    /**
     * 根据账号查找用户信息
     *
     * @param pageNum   当前页
     * @param pageSize  当前页数据个数
     * @param accountNo 账号
     * @param request   用户请求
     * @return 用户信息
     */
    @Override
    public PageInfo<AccountListResponseVO> findAccount(Integer pageNum, Integer pageSize, String accountNo, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<Account> accountLambdaQueryWrapper = new LambdaQueryWrapper<>();
        accountLambdaQueryWrapper
                .like(Account::getAccountNo, accountNo)
                .orderByDesc(Account::getAccountCreateTime);
        List<Account> accountList = accountMapper.selectList(accountLambdaQueryWrapper);
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.like("account_no", accountNo);
        Long accountCountNumber = accountMapper.selectCount(accountQueryWrapper);
        List<AccountListResponseVO> accountListResponseVOList = new ArrayList<>();
        for (Account account : accountList) {
            AccountListResponseVO accountListResponseVO = new AccountListResponseVO();
            accountListResponseVO.setAccountNo(account.getAccountNo());
            accountListResponseVO.setAccountRoleNo(account.getAccountRoleNo());
            Role role = roleMapper.selectById(account.getAccountRoleNo());
            String roleName = role.getRoleName();
            if (Const.ROLE_SYSTEM_ADMIN.equals(roleName)) {
                accountListResponseVO.setAccountRoleName(roleName);
            } else if (Const.ROLE_COMMON_STUDENT.equals(roleName)) {
                accountListResponseVO.setAccountRoleName(roleName);
            } else if (Const.ROLE_COMMON_TEACHER.equals(roleName)) {
                accountListResponseVO.setAccountRoleName(roleName);
            } else if (Const.ROLE_COMMON_BUSINESS.equals(roleName)) {
                accountListResponseVO.setAccountRoleName(roleName);
            } else if (Const.ROLE_COMMON_ORDER_CLASS_ADMIN.equals(roleName)) {
                accountListResponseVO.setAccountRoleName(roleName);
            } else if (Const.ROLE_VISITOR_ADMIN.equals(roleName)) {
                accountListResponseVO.setAccountRoleName(roleName);
            } else if (Const.ROLE_VISITOR_STUDENT.equals(roleName)) {
                accountListResponseVO.setAccountRoleName(roleName);
            } else if (Const.ROLE_VISITOR_TEACHER.equals(roleName)) {
                accountListResponseVO.setAccountRoleName(roleName);
            } else if (Const.ROLE_VISITOR_BUSINESS.equals(roleName)) {
                accountListResponseVO.setAccountRoleName(roleName);
            } else if (Const.ROLE_VISITOR_ORDER_CLASS_ADMIN.equals(roleName)) {
                accountListResponseVO.setAccountRoleName(roleName);
            }
            if ("".equals(account.getAccountPhone())) {
                accountListResponseVO.setAccountPhone("未更新");
            } else {
                accountListResponseVO.setAccountPhone(digestUtils.base64Decode(account.getAccountPhone()));
            }
            if ("".equals(account.getAccountEmail())) {
                accountListResponseVO.setAccountEmail("未更新");
            } else {
                accountListResponseVO.setAccountEmail(digestUtils.base64Decode(account.getAccountEmail()));
            }
            if ("".equals(account.getAccountPhoto())) {
                accountListResponseVO.setAccountPhoto("");
            } else {
                accountListResponseVO.setAccountPhoto(account.getAccountPhoto());
            }
            accountListResponseVO.setAccountCreateMan(account.getAccountCreateMan());
            String createTime = timeUtils.setDateToStrDate(account.getAccountCreateTime(), Const.YYYY_MM_DD);
            accountListResponseVO.setAccountCreateTime(createTime);
            if (account.getAccountUpdateMan() == null) {
                accountListResponseVO.setAccountUpdateMan("未更新");
            } else {
                accountListResponseVO.setAccountUpdateMan(account.getAccountUpdateMan());
            }
            if (account.getAccountUpdateTime() == null) {
                accountListResponseVO.setAccountUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(account.getAccountUpdateTime(), Const.YYYY_MM_DD);
                accountListResponseVO.setAccountUpdateTime(updateTime);
            }
            accountListResponseVO.setAccountBrowser(browserUtils.getBrowserType(request));
            accountListResponseVO.setAccountIPAddress(systemUtils.getIPAddress(request));
            accountListResponseVO.setAccountOSName(systemUtils.getOSName(request));
            accountListResponseVO.setAccountCountNumber(accountCountNumber);
            accountListResponseVOList.add(accountListResponseVO);
        }
        return PageInfo.of(accountListResponseVOList);
    }

    /**
     * 新增账户
     *
     * @param accountRequestVO 账户请求实体类
     * @return 数据库影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addAccount(AccountRequestVO accountRequestVO) {
        int addAccountFlag = 0;
        int addFlag = 0;
        Date createTime = new Date();
        Account account = new Account();
        account.setAccountNo(accountRequestVO.getAccountNo());
        account.setAccountPassword(digestUtils.md5Digest(accountRequestVO.getAccountPassword()));
        account.setAccountRoleNo(accountRequestVO.getAccountRoleNo());
        account.setAccountPhoto("https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png");
        account.setAccountCreateMan(accountRequestVO.getAccountCreateMan());
        account.setAccountCreateTime(createTime);
        Role role = roleMapper.selectById(accountRequestVO.getAccountRoleNo());
        String roleName = role.getRoleName();
        if (Const.ROLE_SYSTEM_ADMIN.equals(roleName)) {
            addAccountFlag = accountMapper.insert(account);
            addFlag = addAccountFlag;
            return addFlag;
        } else if (Const.ROLE_COMMON_STUDENT.equals(roleName)) {
            Student student = new Student();
            student.setStudentNo(accountRequestVO.getAccountNo());
            student.setStudentCreateMan(accountRequestVO.getAccountCreateMan());
            student.setStudentCreateTime(createTime);
            addAccountFlag = accountMapper.insert(account);
            int addStudentFlag = studentMapper.insert(student);
            addFlag = addAccountFlag + addStudentFlag;
            return addFlag;
        } else if (Const.ROLE_COMMON_TEACHER.equals(roleName)) {
            Teacher teacher = new Teacher();
            teacher.setTeacherNo(accountRequestVO.getAccountNo());
            teacher.setTeacherCreateMan(accountRequestVO.getAccountCreateMan());
            teacher.setTeacherCreateTime(createTime);
            addAccountFlag = accountMapper.insert(account);
            int addTeacherFlag = teacherMapper.insert(teacher);
            addFlag = addAccountFlag + addTeacherFlag;
            return addFlag;
        } else if (Const.ROLE_COMMON_BUSINESS.equals(roleName)) {
            Business business = new Business();
            business.setBusinessNo(accountRequestVO.getAccountNo());
            business.setBusinessCreateMan(accountRequestVO.getAccountCreateMan());
            business.setBusinessCreateTime(createTime);
            addAccountFlag = accountMapper.insert(account);
            int addBusinessFlag = businessMapper.insert(business);
            addFlag = addAccountFlag + addBusinessFlag;
            return addFlag;
        } else if (Const.ROLE_COMMON_ORDER_CLASS_ADMIN.equals(roleName)) {
            OrderClassAdmin orderClassAdmin = new OrderClassAdmin();
            orderClassAdmin.setOrderClassAdminNo(accountRequestVO.getAccountNo());
            orderClassAdmin.setOrderClassAdminCreateMan(accountRequestVO.getAccountCreateMan());
            orderClassAdmin.setOrderClassAdminCreateTime(createTime);
            addAccountFlag = accountMapper.insert(account);
            int addOrderClassAdminFlag = orderClassAdminMapper.insert(orderClassAdmin);
            addFlag = addAccountFlag + addOrderClassAdminFlag;
            return addFlag;
        } else if (Const.ROLE_VISITOR_ADMIN.equals(roleName)) {
            addAccountFlag = accountMapper.insert(account);
            addFlag = addAccountFlag;
            return addFlag;
        } else if (Const.ROLE_VISITOR_STUDENT.equals(roleName)) {
            Student student = new Student();
            student.setStudentNo(accountRequestVO.getAccountNo());
            student.setStudentCreateMan(accountRequestVO.getAccountCreateMan());
            student.setStudentCreateTime(createTime);
            addAccountFlag = accountMapper.insert(account);
            int addStudentFlag = studentMapper.insert(student);
            addFlag = addAccountFlag + addStudentFlag;
            return addFlag;
        } else if (Const.ROLE_VISITOR_TEACHER.equals(roleName)) {
            Teacher teacher = new Teacher();
            teacher.setTeacherNo(accountRequestVO.getAccountNo());
            teacher.setTeacherCreateMan(accountRequestVO.getAccountCreateMan());
            teacher.setTeacherCreateTime(createTime);
            addAccountFlag = accountMapper.insert(account);
            int addTeacherFlag = teacherMapper.insert(teacher);
            addFlag = addAccountFlag + addTeacherFlag;
            return addFlag;
        } else if (Const.ROLE_VISITOR_BUSINESS.equals(roleName)) {
            Business business = new Business();
            business.setBusinessNo(accountRequestVO.getAccountNo());
            business.setBusinessCreateMan(accountRequestVO.getAccountCreateMan());
            business.setBusinessCreateTime(createTime);
            addAccountFlag = accountMapper.insert(account);
            int addBusinessFlag = businessMapper.insert(business);
            addFlag = addAccountFlag + addBusinessFlag;
            return addFlag;
        } else if (Const.ROLE_VISITOR_ORDER_CLASS_ADMIN.equals(roleName)) {
            OrderClassAdmin orderClassAdmin = new OrderClassAdmin();
            orderClassAdmin.setOrderClassAdminNo(accountRequestVO.getAccountNo());
            orderClassAdmin.setOrderClassAdminCreateMan(accountRequestVO.getAccountCreateMan());
            orderClassAdmin.setOrderClassAdminCreateTime(createTime);
            addAccountFlag = accountMapper.insert(account);
            int addOrderClassAdminFlag = orderClassAdminMapper.insert(orderClassAdmin);
            addFlag = addAccountFlag + addOrderClassAdminFlag;
            return addFlag;
        }
        return addFlag;
    }

    /**
     * 删除账户
     *
     * @param accountNo 账号
     * @return 数据库影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteAccount(String accountNo) {
        LambdaQueryWrapper<Account> accountLambdaQueryWrapper = new LambdaQueryWrapper<>();
        accountLambdaQueryWrapper.eq(Account::getAccountNo, accountNo);
        Account account = accountMapper.selectOne(accountLambdaQueryWrapper);
        String roleName = "";
        if (account == null) {
            return 0;
        } else {
            Role role = roleMapper.selectById(account.getAccountRoleNo());
            roleName = role.getRoleName();
        }
        int deleteAccountFlag = 0;
        int deleteFlag = 0;
        if (Const.ROLE_SYSTEM_ADMIN.equals(roleName)) {
            deleteAccountFlag = accountMapper.deleteById(accountNo);
            deleteFlag = deleteAccountFlag;
            return deleteFlag;
        } else if (Const.ROLE_COMMON_STUDENT.equals(roleName)) {
            deleteAccountFlag = accountMapper.deleteById(accountNo);
            int deleteStudentFlag = studentMapper.deleteById(accountNo);
            deleteFlag = deleteAccountFlag + deleteStudentFlag;
            return deleteFlag;
        } else if (Const.ROLE_COMMON_TEACHER.equals(roleName)) {
            deleteAccountFlag = accountMapper.deleteById(accountNo);
            int deleteTeacherFlag = teacherMapper.deleteById(accountNo);
            deleteFlag = deleteAccountFlag + deleteTeacherFlag;
            return deleteFlag;
        } else if (Const.ROLE_COMMON_BUSINESS.equals(roleName)) {
            deleteAccountFlag = accountMapper.deleteById(accountNo);
            int deleteBusinessFlag = businessMapper.deleteById(accountNo);
            deleteFlag = deleteAccountFlag + deleteBusinessFlag;
            return deleteFlag;
        } else if (Const.ROLE_COMMON_ORDER_CLASS_ADMIN.equals(roleName)) {
            deleteAccountFlag = accountMapper.deleteById(accountNo);
            int deleteOrderClassAdminFlag = orderClassAdminMapper.deleteById(accountNo);
            deleteFlag = deleteAccountFlag + deleteOrderClassAdminFlag;
            return deleteFlag;
        } else if (Const.ROLE_VISITOR_ADMIN.equals(roleName)) {
            deleteAccountFlag = accountMapper.deleteById(accountNo);
            deleteFlag = deleteAccountFlag;
            return deleteFlag;
        } else if (Const.ROLE_VISITOR_STUDENT.equals(roleName)) {
            deleteAccountFlag = accountMapper.deleteById(accountNo);
            int deleteStudentFlag = studentMapper.deleteById(accountNo);
            deleteFlag = deleteAccountFlag + deleteStudentFlag;
            return deleteFlag;
        } else if (Const.ROLE_VISITOR_TEACHER.equals(roleName)) {
            deleteAccountFlag = accountMapper.deleteById(accountNo);
            int deleteTeacherFlag = teacherMapper.deleteById(accountNo);
            deleteFlag = deleteAccountFlag + deleteTeacherFlag;
            return deleteFlag;
        } else if (Const.ROLE_VISITOR_BUSINESS.equals(roleName)) {
            deleteAccountFlag = accountMapper.deleteById(accountNo);
            int deleteBusinessFlag = businessMapper.deleteById(accountNo);
            deleteFlag = deleteAccountFlag + deleteBusinessFlag;
            return deleteFlag;
        } else if (Const.ROLE_VISITOR_ORDER_CLASS_ADMIN.equals(roleName)) {
            deleteAccountFlag = accountMapper.deleteById(accountNo);
            int deleteOrderClassAdminFlag = orderClassAdminMapper.deleteById(accountNo);
            deleteFlag = deleteAccountFlag + deleteOrderClassAdminFlag;
            return deleteFlag;
        }
        return 0;
    }


    /**
     * 查询管理员个人信息
     *
     * @param accountNo 账号
     * @return 管理员个人信息
     */
    @Override
    public AccountListResponseVO adminInfo(String accountNo) {
        Account account = accountMapper.selectById(accountNo);
        AccountListResponseVO accountListResponseVO = new AccountListResponseVO();
        accountListResponseVO.setAccountNo(accountNo);
        if (account == null) {
            accountListResponseVO.setAccountEmail("");
            accountListResponseVO.setAccountPhone("");
        } else {
            if (account.getAccountPhone() == null) {
                accountListResponseVO.setAccountPhone("");
            } else {
                accountListResponseVO.setAccountPhone(digestUtils.base64Decode(account.getAccountPhone()));
            }
            if (account.getAccountEmail() == null) {
                accountListResponseVO.setAccountEmail("");
            } else {
                accountListResponseVO.setAccountEmail(digestUtils.base64Decode(account.getAccountEmail()));
            }
        }
        return accountListResponseVO;
    }

    /**
     * 文件上传
     *
     * @param file 文件
     * @return 文件路径
     */
    @Override
    public String uploadAvatar(MultipartFile file, String accountNo) {
        String originalFilename = file.getOriginalFilename();  // 获取到原始的文件名   aaa.png   aaa.png
        String uuid = UUID.randomUUID().toString();
        String fileName = uuid + "-" + originalFilename;
        File finalFile = new File(Const.ROOT_AVATAR_PATH + "/" + fileName);  // 最终存到磁盘的文件对象
        if (!finalFile.getParentFile().exists()) {  // 如果父级目录不存在 就得创建
            finalFile.getParentFile().mkdirs();
        }
        try {
            file.transferTo(finalFile);
        } catch (IOException ioException) {
            log.error("文件路径不存在：" + ioException.getMessage(), ioException);
        }
        String accountPhoto = "http://" + ip + ":" + port + "/files/download?fileName=" + fileName;
        Account photo = new Account();
        photo.setAccountNo(accountNo);
        photo.setAccountPhoto(accountPhoto);
        photo.setAccountUpdateMan(accountNo);
        photo.setAccountUpdateTime(new Date());
        accountMapper.updateById(photo);
        // 返回文件的url
        return accountPhoto;
    }

    /**
     * 文件下载
     *
     * @param fileName 文件名
     * @param response 响应
     * @return 是否下载成功
     */
    @Override
    public boolean downloadAvatar(String fileName, HttpServletResponse response) {
        File file = new File(Const.ROOT_AVATAR_PATH + "/" + fileName);
        ServletOutputStream servletOutputStream = null;
        try {
            servletOutputStream = response.getOutputStream();
        } catch (IOException ioException) {
            log.error("头像下载异常：" + ioException.getMessage(), ioException);
        }
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
        response.setContentType("application/octet-stream");
//        if (servletOutputStream != null) {
//            try {
//                servletOutputStream.write(FileUtil.readBytes(file));
//            } catch (IOException ioException) {
//                log.error("获取头像异常：" + ioException.getMessage(), ioException);
//            }
//        }
        FileUtil.writeToStream(file, servletOutputStream);
        try {
            if (servletOutputStream != null) {
                servletOutputStream.flush();
            }
        } catch (IOException ioException) {
            log.error("flushServletOutputStream资源异常：" + ioException.getMessage(), ioException);
        }
        try {
            if (servletOutputStream != null) {
                servletOutputStream.close();
            }
        } catch (IOException ioException) {
            log.error("释放servletOutputStream资源异常：" + ioException.getMessage(), ioException);
        }
        return true;
    }

    /**
     * 修改个人信息
     *
     * @param accountRequestVO 账户请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int updateInfo(AccountRequestVO accountRequestVO) {
        Account account = new Account();
        account.setAccountNo(accountRequestVO.getAccountNo());
        account.setAccountPhone(digestUtils.base64Encode(accountRequestVO.getAccountPhone()));
        account.setAccountEmail(digestUtils.base64Encode(accountRequestVO.getAccountEmail()));
        account.setAccountUpdateMan(accountRequestVO.getAccountNo());
        account.setAccountUpdateTime(new Date());
        return accountMapper.updateById(account);
    }

    /**
     * 用户注册
     *
     * @param accountRequestVO 用户请求实体类
     * @return 数据库影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int accountRegister(AccountRequestVO accountRequestVO) {
        Date registerTime = new Date();
        Account account = new Account();
        account.setAccountNo(accountRequestVO.getAccountNo());
        account.setAccountPassword(digestUtils.md5Digest(accountRequestVO.getAccountPassword()));
        account.setAccountRoleNo(4);
        account.setAccountPhoto("https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png");
        account.setAccountCreateMan(accountRequestVO.getAccountNo());
        account.setAccountCreateTime(registerTime);
        int registerAccountFlag = accountMapper.insert(account);
        Business business = new Business();
        business.setBusinessNo(accountRequestVO.getAccountNo());
        business.setBusinessName(accountRequestVO.getAccountBusinessName());
        business.setBusinessOrderClassHeadName(accountRequestVO.getAccountBusinessOrderClassHeadName());
        business.setBusinessCreateMan(accountRequestVO.getAccountNo());
        business.setBusinessCreateTime(registerTime);
        int registerBusinessFlag = businessMapper.insert(business);
        return registerAccountFlag + registerBusinessFlag;
    }

    /**
     * 查询账号列表
     *
     * @return 账号列表
     */
    @Override
    public List<String> accountNoList() {
        LambdaQueryWrapper<Account> accountLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Account> accountList = accountMapper.selectList(accountLambdaQueryWrapper);
        List<String> accountNoList = new ArrayList<>();
        for (Account account : accountList) {
            accountNoList.add(account.getAccountNo());
        }
        return accountNoList;
    }

    /**
     * 用户重置
     *
     * @param accountRequestVO 用户请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int accountReset(AccountRequestVO accountRequestVO) {
        Account account = new Account();
        account.setAccountNo(accountRequestVO.getAccountNo());
        account.setAccountPassword(digestUtils.md5Digest(accountRequestVO.getAccountPassword()));
        account.setAccountUpdateMan(accountRequestVO.getAccountNo());
        account.setAccountUpdateTime(new Date());
        return accountMapper.updateById(account);
    }
}
